package com.zndroid.gridview.preview;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.zndroid.gridview.IImageBean;
import com.zndroid.gridview.PhotoImageView;
import com.zndroid.gridview.R;
import com.zndroid.gridview.load.IImageDisplayEngine;
import com.zndroid.gridview.load.ILoadCallBack;
import com.zndroid.gridview.load.ImageLoader;
import com.zndroid.gridview.inner.gesture.OnPhotoTapListener;

import java.lang.ref.WeakReference;
import java.util.List;

public class ImagePreviewAdapter extends PagerAdapter implements OnPhotoTapListener {
    private final List<ImageAttribute> imageList;
    private IImageDisplayEngine engine;
    private View currentView;
    private final WeakReference<Context> context;
    private PreviewCallBack callBack;

    public interface PreviewCallBack {
        /**
         * when clicked will be callback
         * */
        void onTapClicked();
    }

    public ImagePreviewAdapter(@NonNull Context context, @NonNull List<ImageAttribute> imageList, @NonNull IImageDisplayEngine engine) {
        this.imageList = imageList;
        this.context = new WeakReference<>(context);
        this.engine = engine;
    }

    public void setCallBack(PreviewCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        super.setPrimaryItem(container, position, object);
        currentView = (View) object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(context.get()).inflate(R.layout.layout_gil_preview_item, container, false);
        final ProgressBar pb = (ProgressBar) view.findViewById(R.id.iv_gil_progress);
        final PhotoImageView imageView = view.findViewById(R.id.iv_gil_preview);

        if (null == engine) {
            throw new UnsupportedOperationException("image load engine is null, pls impl it at first.");
        }

        ImageAttribute info = this.imageList.get(position);
        imageView.setOnPhotoTapListener(this);
        showExcessPic(info.getImageData(), imageView);

        if (null == engine.getLoadCallBack() && engine.isOpenInnerProgress()) {
            pb.setVisibility(View.VISIBLE);
            ImageLoader.getLoader().load(context, imageView, engine, info.getImageData(), new ILoadCallBack() {
                @Override
                public void onLoadSuccess() {
                    pb.setVisibility(View.GONE);
                }

                @Override
                public void onLoadFailed() {
                    pb.setVisibility(View.GONE);
                }
            });
        } else {
            ImageLoader.getLoader().load(context, imageView, engine, info.getImageData(), engine.getLoadCallBack());
        }

        container.addView(view);

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    public View getPrimaryItem() {
        return currentView;
    }

    public ImageView getPrimaryImageView() {
        return (ImageView) currentView.findViewById(R.id.iv_gil_preview);
    }

    /** 展示过度图片 */
    private void showExcessPic(IImageBean imageInfo, ImageView imageView) {
        if (null == imageInfo) {
            imageView.setImageResource(R.drawable.ic_gil_default_color);
            return;
        }

        //先获取大图的缓存图片
        Bitmap cacheImage = engine.getCacheImage(imageInfo.supplyBigImageUrl());
        //如果大图的缓存不存在,在获取小图的缓存
        if (cacheImage == null) {
            cacheImage = engine.getCacheImage(imageInfo.supplyThumbnailUrl());
        }
        //如果没有任何缓存,使用默认图片,否者使用缓存
        if (cacheImage == null) {
            imageView.setImageResource(R.drawable.ic_gil_default_color);
        } else {
            imageView.setImageBitmap(cacheImage);
        }
    }

    @Override
    public void onPhotoTap(ImageView view, float x, float y) {
        if (null != callBack) {
            callBack.onTapClicked();
        }
    }
}
